import request from "@/utils/request";

const AuthAPI = {
  /** 密码登录接口*/
  login(data: LoginFormData) {
    const formData = new FormData();
    formData.append("mobile", data.userName);
    formData.append("password", data.password);
    return request<any, LoginResult>({
      url: `/crmauth/crmLogin`,
      method: "post",
      data: formData,
    });
  },
  /** 检查数据是否需要迁移*/
  checkMove() {
    return request<any, checkMoveResult>({
      url: `/crmauth/checkMove`,
      method: "get",
    });
  },
  /**
   * 获取当前登录用户信息
   *
   * @returns 登录用户昵称、头像信息，包括角色和权限
   */
  getInfo() {
    return request<any, UserInfo>({
      url: `/crmauth/me`,
      method: "get",
    });
  },
  /** 注销登录接口 */
  logout() {
    return request({
      url: `/auth/logout`,
      method: "post",
    });
  },
  /**
   * 获取验证码接口
   * @param data - 登录表单数据
   */
  getMobileCode(data: LoginFormData) {
    return request({
      url: `/crm/migration/sendCode`,
      method: "post",
      data: data,
    });
  },
};

export default AuthAPI;

/** 登录表单数据 */
export interface LoginFormData {
  /** 用户名 */
  userName: string;
  /** 密码 */
  password: string;
  /** 手机验证码 */
  code: string;
  /** 手机号 */
  mobile: string;
}

/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  token: string;
  /** 过期时间 */
  expires_in: string;
}
/** 检查是否迁移响应 */
export interface checkMoveResult {
  /** 员工名称 */
  employeeName: string;
  /** 是否迁移 */
  isMoveOver: number;
}

/** 登录用户信息 */
export interface UserInfo {
  balance: string;
  companyId: number;
  createdAt: string;
  deferTime: string;
  departureTime: string;
  description: string;
  enableActivationCenter: number;
  enableResignationSea: number;
  enableServiceCenter: number;
  entryTime: string;
  gainTime: number;
  gender: number;
  groupId: number;
  headImg: string;
  id: number;
  idCard: string;
  isMove: number;
  isShowHow: number;
  lockedNum: number;
  mobile: string;
  name: string;
  opePassword: string;
  permissions: string[];
  permissionsArray: string[];
  positionId: number;
  reLogin: number;
  roleId: number;
  roleType: number;
  roles: RolesInfo[];
  salt: string;
  specialRoleType: number;
  status: number;
  teamId: number;
  totalIncome: string;
  tryTime: number;
  updatedAt: string;
  username: string;
  withdrawedMoney: string;
  wxNickName: string;
  wxOpenId: string;
}
/** 角色信息 */
export interface RolesInfo {
  created_at: string;
  guard_name: string;
  id: number;
  name: number;
  permissions: string[];
  pivot: PivotInfo;
  updated_at: string;
}

export interface PivotInfo {
  model_id: number;
  role_id: number;
}
